﻿using System;

namespace Red.Launcher.ViewModels.Workspaces.Database.Sql
{
    #region Interfaces

    /// <summary>
    /// An interface for a class that can output either a complete or partial sql query
    /// </summary>
    public interface ISql
    {
        string Sql { get; }
    }

    /// <summary>
    /// An interface for a class that can output SQL that can be referenced by shorthand later on
    /// </summary>
    public interface ISqlDeclaration
    {
        string SqlDeclaration { get; }
    }

    /// <summary>
    /// An identifier used for referencing a section of sql
    /// </summary>
    public interface IAlias
    {
        string Alias { get; set; }
    }
    #endregion Interfaces

    #region Extension methods

    /// <summary>
    /// These functions exist because you cannot add functions
    /// to interfaces directly, but you can via an extension
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// When declaring a potentially aliased bit of SQL, this is used to add the reference
        /// </summary>
        public static string GetAliasDeclaration( this IAlias alias )
        {
            if ( String.IsNullOrEmpty( alias.Alias ) )
            {
                return "";
            }

            return String.Format( " AS {0}", alias.Alias );
        }

        /// <summary>
        /// Used when referencing something aliased elsewhere
        /// </summary>
        public static string GetAliasPrefix( this IAlias alias )
        {
            if ( alias == null || String.IsNullOrEmpty( alias.Alias ) )
            {
                return "";
            }

            return String.Format( "{0}.", alias.Alias );
        }
    }

    #endregion Extension methods
}
